package com.property.manage.service.renovation;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.guardchina.framework.util.utils.DateUtil;
import com.guardchina.framework.util.utils.NumberUtil;
import com.guardchina.framework.util.utils.PhoneUtil;
import com.property.manage.dao.flowingWater.FlowingWaterMapper;
import com.property.manage.dao.renovation.RenovationMapper;
import com.property.manage.model.flowingWater.FlowingMoneyVo;
import com.property.manage.model.flowingWater.FlowingWaterEntity;
import com.property.manage.model.flowingWater.FlowingWaterVo;
import com.property.manage.model.renovation.DepositEntity;
import com.property.manage.model.renovation.RenovationEntity;
import com.property.manage.model.renovation.RenovationVo;
import com.property.manage.model.resource.Building;
import com.property.manage.model.resource.BuildingVo;
import com.property.manage.service.fee.PropertyService;
import com.property.manage.service.rent.RentService;
import com.property.manage.service.resource.BuildingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Component
@Slf4j
public class RenovationService {
    @Autowired
    private RenovationMapper renovationMapper;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private DepositService depositService;

    public RenovationVo findById(Integer id) {
        return renovationMapper.queryById(id);
    }

    public CustomPage<RenovationVo> page(Long pageNo, Long pageSize, Integer villageId, Integer building, Integer unit, String roomNumber,String startTime, String endTime) {
        Page<RenovationVo> page = new Page<>(pageNo, pageSize);
        IPage<RenovationVo> village = renovationMapper.getPage(page, villageId, building, unit, roomNumber, startTime, endTime);
        List<RenovationVo> records = village.getRecords();
        if (!records.isEmpty()){
            for (RenovationVo record : records) {
                record.setAddress(buildingService.findAddress(record.getBuildingId()));
            }
        }
        return new CustomPage<>(village);
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(BigDecimal money, LocalDate startTime, LocalDate endTime, String name,
                    String phone, Integer buildingId, Integer decorationStatus, Integer userId

    ) {
        if (!PhoneUtil.isMobile(phone)) {
            throw new BusinessException("请填写正确的手机号码", String.format("手机号码错误[%s]", phone));
        }
        BuildingVo vo = buildingService.findBuilding(buildingId);
        if (vo==null){
            throw new BusinessException("楼栋信息错误", String.format("楼栋信息错误[%s]", buildingId));
        }
        RenovationEntity entity = new RenovationEntity();
        entity.setMoney(money);
        entity.setPhone(phone);
        entity.setBuildingId(buildingId);
        entity.setVillageId(vo.getVillageId());
        entity.setDecorationStatus(decorationStatus);
        entity.setEndTime(endTime);
        entity.setStartTime(startTime);
        entity.setName(name);
        try {
            renovationMapper.insert(entity);
            //将数据同步到装修押金管理中
            //BuildingVo building = getBuildingVo(buildingId);
           // depositService.add(money,getString(building,name),phone,buildingId,entity.getId(),building.getVillageId(),1,userId,null);
        } catch (Exception e) {
            log.info("新增装修登记信息错误", String.format("装修登记错误[%S]", e));
            throw new BusinessException("登记失败");
        }
    }

    private BuildingVo getBuildingVo(Integer buildingId) {
        BuildingVo building = buildingService.findBuilding(buildingId);
        if (building==null){
            throw new BusinessException("楼栋信息错误",String.format("楼栋id[%s]",buildingId));
        }
        return building;
    }

    private String getString(BuildingVo building,String name) {

        StringBuilder sb=new StringBuilder();
        sb.append(building.getVillageName()).append("小区")
                .append(building.getBuilding()).append("栋").append(building.getUnit()).append("单元")
        .append(building.getRoomNumber()).append(name);
        return sb.toString();

    }

    @Transactional(rollbackFor = Exception.class)
    public void update(Integer id, BigDecimal money, LocalDate startTime, LocalDate endTime, String name,
                       String phone, Integer buildingId, Integer decorationStatus
    ) {
        BuildingVo bvo = buildingService.findBuilding(buildingId);
        if (bvo==null){
            throw new BusinessException("楼栋信息错误", String.format("楼栋信息错误[%s]", buildingId));
        }
        UpdateWrapper<RenovationEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("money", money);
        wrapper.set("start_time", startTime);
        wrapper.set("end_time", endTime);
        wrapper.set("name", name);
        wrapper.set("phone", phone);
        wrapper.set("building_id", buildingId);
        wrapper.set("decoration_status", decorationStatus);
        wrapper.set("village_id",bvo.getVillageId());
        wrapper.eq("id", id);
        renovationMapper.update(null, wrapper);
    }


    public void delete(Integer id) {
        renovationMapper.deleteById(id);
    }

   public void updatStatus(Integer id,Integer depositStatus){
       UpdateWrapper<RenovationEntity> wrapper = new UpdateWrapper<>();
       wrapper.set("deposit_status", depositStatus);
       wrapper.eq("id", id);
           renovationMapper.update(null, wrapper);
   }


}
